﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

static public class StaticFunction
{
    static public T FindInChild<T>(this GameObject parent, string path = null) where T : Component
    {
        if (string.IsNullOrEmpty(path))
        {
            return parent.GetComponent<T>();
        }
        Transform trans = parent.transform.Find(path);
        if (trans == null)
            return null;
        else
            return trans.gameObject.GetComponent<T>();

    }

    public static GameObject FindChild(this GameObject parent, string path)
    {
        Transform trans = parent.transform;
        if (trans.Find(path))
            return trans.Find(path).gameObject;
        else
            return null;
    }

    static public T AddMissingComponent<T>(this GameObject go) where T : Component
    {
        T comp = go.GetComponent<T>();
        if (comp == null)
        {
            comp = go.AddComponent<T>();
        }
        return comp;
    }

    /// <summary>
    /// 把格子坐标转成世界坐标 xy转成x0z
    /// </summary>
    static public Vector3 To3D(this Vector2 pos2D)
    {
        return new Vector3(pos2D.x, 0, pos2D.y);
    }
}
